package com.chanyachting.emall.action.lease;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Company;
import com.chanyachting.emall.domain.Companymember;
import com.chanyachting.emall.domain.Newsactivity;
import com.chanyachting.emall.form.CompanyForm;
import com.chanyachting.emall.service.CompanyMemberService;
import com.chanyachting.emall.service.CompanyService;
import com.chanyachting.emall.service.NewsActivityService;
import com.chanyachting.emall.serviceImpl.CompanyMemberServiceImpl;
import com.chanyachting.emall.serviceImpl.CompanyServiceImpl;
import com.chanyachting.emall.serviceImpl.NewsActivityServiceImpl;

public class ReadCompanyOfNewsListAction  extends Action{
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		CompanyService comService = new CompanyServiceImpl();
		CompanyMemberService compMemService = new CompanyMemberServiceImpl();
		NewsActivityService newsService = new NewsActivityServiceImpl();

		CompanyForm tForm = (CompanyForm)form;
		if(tForm.getId() == null) {
			return mapping.findForward("error");
		}
		
		List<Company> alList = null;
		
		alList = comService.getCompanyById(tForm.getId().toString());
		if (alList.size() == 0) {
			return mapping.findForward("error");
		}
		
		List<Companymember> compMemList = compMemService.getCompanymemberByCompany(alList.get(0));
		
		if (compMemList == null || compMemList.size() == 0) {
			return mapping.findForward("error");
		}
		
		List<Newsactivity> newsList = new ArrayList<Newsactivity>();
		
		for (Companymember cm:compMemList) {
			List<Newsactivity> tmpNewsList = newsService.getMemberReleasedNewsActivity(cm.getMemberId());
			
			if (tmpNewsList == null || tmpNewsList.size() == 0) {
				continue;
			}
			
			for (Newsactivity n:tmpNewsList) {
				String tmpContent = n.getContext();
				
				if (tmpContent != null && !"".equals(tmpContent) && tmpContent.length() > 150) {
					tmpContent = tmpContent.substring(0,150);
					tmpContent.concat(".....");
				}
				
				n.setContext(tmpContent);
				newsList.add(n);
			}
		}
		tForm.setNewsList(newsList);
		tForm.setComp(alList.get(0));
		return mapping.findForward("readRentCompanyNews");
	}
}
